package response

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

//统一返回的格式,
//结构还需要优化下。
//{
//	"code":00
//	"data":{}
//	"msg":""
//}
func Response(ctx *gin.Context, httpStatus int, code int, data gin.H, msg string) {
	jsonData := gin.H{
		"code": code,
		"msg":  msg,
		"data": data,
	}
	jsonStr, err := json.Marshal(jsonData)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, "系统异常")
		return
	}
	log.Printf("返回的数据：%v", string(jsonStr))
	ctx.JSON(httpStatus, jsonData)
}

func Success(ctx *gin.Context, data gin.H, msg string) {
	Response(ctx, http.StatusOK, 200, data, msg)
}

func OnFail(ctx *gin.Context, data gin.H, msg string) {
	Response(ctx, http.StatusOK, 400, data, msg)
}
